package br.com.bb.robot.model;

import com.ibm.eNetwork.ECL.ECLOIA;
import com.ibm.eNetwork.ECL.ECLPS;
import com.ibm.eNetwork.ECL.ECLSession;
import com.ibm.eNetwork.HOD.customizable.CustomDesktop;
import com.ibm.eNetwork.HOD.customizable.HODDisplaySession;
import com.ibm.eNetwork.HOD.customizable.HODSessionManager;
import com.ibm.eNetwork.beans.HOD.Terminal;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.*;

public class DisplaySession extends JFrame {
    JMenuBar menuBar = new JMenuBar();
    CustomDesktop desktop = null;
    HODDisplaySession session = null;   
    
    Terminal terminal = null;
    ECLSession eSession = null;
    ECLPS ps = null;
    ECLOIA oia;     

    public void initialize() {
        HODSessionManager sm = new HODSessionManager();
        try {
            File file = new File("C:\\Documents and Settings\\Administrador\\Meus documentos");
            desktop = sm.createCustomDesktop(this, "http://sisbb.bb.com.br/hod/", "sisbb");
        } catch (Exception e) {
        }
        buildMenus();
        setJMenuBar(menuBar);
        if (desktop != null) {
            createTerminal();
        }
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                myExit();
            }
        });
        Toolkit toolkit = this.getToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        this.setSize(screenSize.width * 2 / 3, screenSize.height * 2 / 3);
        this.setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);         
        this.setVisible(true);
        this.pack();     
        terminal = session.getTerminal();
        eSession = terminal.getECLSession();
        ps = eSession.GetPS();
        oia = eSession.GetOIA();
        //TODO Código aqui abaixo.          
    }

    public void buildMenus() {
        JMenu menu = new JMenu("File");
        JMenuItem menuItem = new JMenuItem("Exit");
        menu.add(menuItem);
        menuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                myExit();
            }
        });
        menuBar.add(menu);
    }

    public void createTerminal() {
        try {
            session = desktop.startDisplaySession("SISBB Intranet");
            if (session != null) {
                this.getContentPane().add(session.getTerminal());
                JMenu menu = new JMenu("HOD Session");
                menuBar.add(menu);
                JMenuBar hodMenuBar = session.getHODMenubar();
                MenuElement[] menuElements = hodMenuBar.getSubElements();
                int count = hodMenuBar.getMenuCount();
                for (int i = 0; i < menuElements.length; i++) {
                    menu.add((JMenu) menuElements[i]);
                }
            }
        } catch (Exception e) {
        }
    }

    public void myExit() {
        if (desktop != null) {
            desktop.closeAllSessions();
        }
        System.exit(0);
    }
    
     
}